package me.sealer.ssm.service.impl;

import lombok.extern.slf4j.Slf4j;
import me.sealer.ssm.constant.ErrorCodeConstant;
import me.sealer.ssm.mapper.RoleMapper;
import me.sealer.ssm.mapper.UserMapper;
import me.sealer.ssm.exception.SsmException;
import me.sealer.ssm.model.Role;
import me.sealer.ssm.model.User;
import me.sealer.ssm.service.RoleService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author sealer
 * @date 2018/05/11.
 */
@Slf4j
@Service
public class RoleServiceImpl implements RoleService {
    private final RoleMapper roleMapper;
    private final UserMapper userMapper;

    public RoleServiceImpl(RoleMapper roleMapper, UserMapper userMapper) {
        this.roleMapper = roleMapper;
        this.userMapper = userMapper;
    }

    @Override
    public List<Role> getRolesByUsername(String username) {
        User user = userMapper.findUserByUsername(username);
        if (user == null) {
            throw new SsmException(ErrorCodeConstant.USER_USER_NOT_EXISTS, new Object[] {username});
        }
        Integer id = user.getId();
        List<Role> roles = roleMapper.getRolesByUserId(id);
        return roles;
    }

    @Override
    public Role getRoleByRoleName(String roleName) {
        return roleMapper.getRoleByName(roleName);
    }

    @Override
    public List<Role> getRoles() {
        return roleMapper.selectByExample(null);
    }


}
